<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 基础控制器
 * +----------------------------------------------------------------------
 */
declare (strict_types = 1);

namespace app\index\controller\admin;

use think\App;
use think\facade\Db;
use think\facade\Request;

class Base
{
    /**
   * Request实例
   * @var \think\Request
   */
  protected $request;

  /**
   * 应用实例
   * @var \think\App
   */
  protected $app;

  /**
   * 构造方法
   * @access public
   * @param  App  $app  应用对象
   */
  public function __construct(App $app)
  {
    $this->app     = $app;
    $this->request = $this->app->request;

  }

  /**
   * 设置一条或者多条数据的状态
   * @param $strict 严格模式要求处理的纪录的uid等于当前登陆用户UID
   * @author jry <598821125@qq.com>
   */
  public function setStatus($model, $status)
  {
    if (!$model && !$status) {
      return ['error' => 1, 'msg' => '参数错误！'];
    }

    $ids = Request::param('id');
    if (is_array($ids)) {
      $ids = explode(',', $ids);
    }

    switch ($status) {
      case 'delete': // 删除记录
        // 查询当前删除的项目是否有子代
        $db = Db::getConnection();
        $Fields = $db->getFields('tp_'.$model);
        $ishave = 0;
        foreach($Fields as $key =>$r) {
          if($key == 'pid') $ishave = 1;
        }
        if ($ishave) {
          $count = Db::name($model)->where('pid', 'in', $ids)->count();
          if ($count > 0) {
            return ['error' => 1, 'msg' => '无法删除，存在子项目！'];
          }
        }

        // 删除记录
        $result = Db::name($model)->delete($ids);
        if ($result) {
          return ['error' => 0, 'msg' => '删除成功，不可恢复！'];
        } else {
          return ['error' => 1, 'msg' => '删除失败!'];
        }
        break;
      case 'status': // 删除记录
        if(!empty($ids)) {
          Db::name($model)
          ->where('id', 'in', $ids)
          ->update(['status' => Request::param('status')]);
          return ['error' => 0, 'msg' => '操作成功！'];
        }
        else {
          return ['error' => 1, 'msg' => '参数错误！'];
        }
        break;
      default:
        return ['error' => 1, 'msg' => '参数错误!'];
        break;
    }
  }

}
?>